//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "ExtendedBlackVarianceCurve.h"
using namespace Cephei::QL::Experimental::Volatility;
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Quote.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Termstructures/Volatility/Equityfx/BlackVarianceTermStructure.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL;
using namespace Cephei::QL::Termstructures::Volatility::Equityfx;
#define HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Volatility::CExtendedBlackVarianceCurve::CExtendedBlackVarianceCurve (boost::shared_ptr<QuantLib::ExtendedBlackVarianceCurve>& childNative, Object^ owner) : CBlackVarianceTermStructure(CExtendedBlackVarianceCurve::typeid)
{
#ifdef HANDLE
	_phExtendedBlackVarianceCurve = NULL;
#endif
	_ppExtendedBlackVarianceCurve = &childNative;
    _ppBlackVarianceTermStructure = new boost::shared_ptr<QuantLib::BlackVarianceTermStructure> (boost::dynamic_pointer_cast<QuantLib::BlackVarianceTermStructure> (*_ppExtendedBlackVarianceCurve));
}
Cephei::QL::Experimental::Volatility::CExtendedBlackVarianceCurve::CExtendedBlackVarianceCurve (QuantLib::ExtendedBlackVarianceCurve& childNative, Object^ owner) : CBlackVarianceTermStructure(CExtendedBlackVarianceCurve::typeid)
{
#ifdef HANDLE
	_phExtendedBlackVarianceCurve = NULL;
#endif
	_ppExtendedBlackVarianceCurve = new boost::shared_ptr<QuantLib::ExtendedBlackVarianceCurve> (&childNative);
    _ppBlackVarianceTermStructure = new boost::shared_ptr<QuantLib::BlackVarianceTermStructure> (boost::dynamic_pointer_cast<QuantLib::BlackVarianceTermStructure> (*_ppExtendedBlackVarianceCurve));
    _ExtendedBlackVarianceCurveOwner = owner;
    _BlackVarianceTermStructureOwner = owner;
}

Cephei::QL::Experimental::Volatility::CExtendedBlackVarianceCurve::CExtendedBlackVarianceCurve (CExtendedBlackVarianceCurve^ copy) : CBlackVarianceTermStructure(CExtendedBlackVarianceCurve::typeid)
{
#ifdef HANDLE
	_phExtendedBlackVarianceCurve = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppExtendedBlackVarianceCurve = new boost::shared_ptr<QuantLib::ExtendedBlackVarianceCurve> (copy->GetShared());
        _ppBlackVarianceTermStructure = new boost::shared_ptr<QuantLib::BlackVarianceTermStructure> (boost::dynamic_pointer_cast<QuantLib::BlackVarianceTermStructure> (*_ppExtendedBlackVarianceCurve));
    }
}
Cephei::QL::Experimental::Volatility::CExtendedBlackVarianceCurve::CExtendedBlackVarianceCurve (PLATFORM::Type^ t) : CBlackVarianceTermStructure(CExtendedBlackVarianceCurve::typeid)
{
#ifdef HANDLE
	_phExtendedBlackVarianceCurve = NULL;
#endif
	if (!t->IsSubclassOf(CExtendedBlackVarianceCurve::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Volatility::CExtendedBlackVarianceCurve::CExtendedBlackVarianceCurve (QuantLib::Handle<QuantLib::ExtendedBlackVarianceCurve>& childNative, Object^ owner)  : CBlackVarianceTermStructure(CExtendedBlackVarianceCurve::typeid)
{
	_phExtendedBlackVarianceCurve = &childNative;
	_ppExtendedBlackVarianceCurve = &static_cast<boost::shared_ptr<QuantLib::ExtendedBlackVarianceCurve>>(childNative.currentLink());
    _ppBlackVarianceTermStructure = new boost::shared_ptr<QuantLib::BlackVarianceTermStructure> (boost::dynamic_pointer_cast<QuantLib::BlackVarianceTermStructure> (*_ppExtendedBlackVarianceCurve));
    _ExtendedBlackVarianceCurveOwner = owner;
}
Cephei::QL::Experimental::Volatility::CExtendedBlackVarianceCurve::CExtendedBlackVarianceCurve (QuantLib::Handle<QuantLib::ExtendedBlackVarianceCurve> childNative)  : CBlackVarianceTermStructure(CExtendedBlackVarianceCurve::typeid)
{
	_phExtendedBlackVarianceCurve = &childNative;
	_ppExtendedBlackVarianceCurve = &static_cast<boost::shared_ptr<QuantLib::ExtendedBlackVarianceCurve>>(childNative.currentLink());
    _ppBlackVarianceTermStructure = new boost::shared_ptr<QuantLib::BlackVarianceTermStructure> (boost::dynamic_pointer_cast<QuantLib::BlackVarianceTermStructure> (*_ppExtendedBlackVarianceCurve));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Volatility::CExtendedBlackVarianceCurve::CExtendedBlackVarianceCurve (QuantLib::ExtendedBlackVarianceCurve childNative)  : CBlackVarianceTermStructure(CExtendedBlackVarianceCurve::typeid)
{
#ifdef HANDLE
	_phExtendedBlackVarianceCurve = NULL;
#endif
	_ppExtendedBlackVarianceCurve = new boost::shared_ptr<QuantLib::ExtendedBlackVarianceCurve> (new QuantLib::ExtendedBlackVarianceCurve (childNative));
    _ppBlackVarianceTermStructure = new boost::shared_ptr<QuantLib::BlackVarianceTermStructure> (boost::dynamic_pointer_cast<QuantLib::BlackVarianceTermStructure> (*_ppExtendedBlackVarianceCurve));
}
#endif

Cephei::QL::Experimental::Volatility::CExtendedBlackVarianceCurve::~CExtendedBlackVarianceCurve ()
{
    if (_ppExtendedBlackVarianceCurve != NULL)
    {
	    delete _ppExtendedBlackVarianceCurve;
        _ppExtendedBlackVarianceCurve = NULL;
    }
}
Cephei::QL::Experimental::Volatility::CExtendedBlackVarianceCurve::!CExtendedBlackVarianceCurve ()
{
    if (_ppExtendedBlackVarianceCurve != NULL)
    {
	    delete _ppExtendedBlackVarianceCurve;
    }
}
QuantLib::ExtendedBlackVarianceCurve& Cephei::QL::Experimental::Volatility::CExtendedBlackVarianceCurve::GetReference ()
{
    if (_ppExtendedBlackVarianceCurve == NULL) throw REFNEW NativeNullException ();
	return **_ppExtendedBlackVarianceCurve;
}
boost::shared_ptr<QuantLib::ExtendedBlackVarianceCurve>& Cephei::QL::Experimental::Volatility::CExtendedBlackVarianceCurve::GetShared ()
{
    if (_ppExtendedBlackVarianceCurve == NULL) throw REFNEW NativeNullException ();
	return *_ppExtendedBlackVarianceCurve;
}
QuantLib::ExtendedBlackVarianceCurve* Cephei::QL::Experimental::Volatility::CExtendedBlackVarianceCurve::GetPointer ()
{
    if (_ppExtendedBlackVarianceCurve == NULL) throw REFNEW NativeNullException ();
	return &**_ppExtendedBlackVarianceCurve;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::ExtendedBlackVarianceCurve>& Cephei::QL::Experimental::Volatility::CExtendedBlackVarianceCurve::GetHandle ()
{
	if (_phExtendedBlackVarianceCurve == NULL)
	{
		_phExtendedBlackVarianceCurve = new Handle<QuantLib::ExtendedBlackVarianceCurve> (*_ppExtendedBlackVarianceCurve);
	}
	return *_phExtendedBlackVarianceCurve;
}
#endif
bool Cephei::QL::Experimental::Volatility::CExtendedBlackVarianceCurve::HasNative () 
{
	return (_ppExtendedBlackVarianceCurve != NULL);
}

Cephei::QL::Times::IDayCounter^ Cephei::QL::Experimental::Volatility::CExtendedBlackVarianceCurve::DayCounter::get ()
{
    try
    {
    	QuantLib::DayCounter _rv = (QuantLib::DayCounter)(*_ppExtendedBlackVarianceCurve)->dayCounter ( );   
        Cephei::QL::Times::CDayCounter^ _nrv = REFNEW Cephei::QL::Times::CDayCounter (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Experimental::Volatility::CExtendedBlackVarianceCurve::MaxDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppExtendedBlackVarianceCurve)->maxDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Volatility::CExtendedBlackVarianceCurve::MaxStrike::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppExtendedBlackVarianceCurve)->maxStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Volatility::CExtendedBlackVarianceCurve::MinStrike::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppExtendedBlackVarianceCurve)->minStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Volatility::IExtendedBlackVarianceCurve^ Cephei::QL::Experimental::Volatility::CExtendedBlackVarianceCurve::Update::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppExtendedBlackVarianceCurve)->update ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

